<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\Config;
use think\Db;
use think\Hook;

/**
 * 公共接口
 */
class Daili extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    /**
     *  用户是否申请过代理
     */
    public function isdiali(){

        $user=$this->auth->getUser();

        $logs=Db::name('daili')->where(['user_id'=>$user['id']])->find();


        if (!$logs){
            $this->success('不是代理',['status'=>0]);
        }
        if ($logs['status']!=1){
            $this->success('申请中',['status'=>2]);
        }

        $this->success('代理',['status'=>1]);



    }



    /**
    用户提交申请
     */
    public function dailiapply(){


        $user=$this->auth->getUser();

        $data['name']=$this->request->param('name');
        $data['mobile']=$this->request->param('mobile');
        $data['address_id']=$this->request->param('city_id');
        $data['imgb']=$this->request->param('imgb');
        $data['imga']=$this->request->param('imga');
        $data['user_id']=$user['id'];

        $logs=Db::name('daili')->where(['user_id'=>$user['id']])->find();

        if ($logs){
            $this->error('请等待审核');
        }

        Db::name('daili')->data($data)->insert();

        $this->success('请等待审核');



    }


    /**
     * 我的金额
     */
    public function getmymoney(){

        $user=$this->auth->getUser();

        $money=Db::name('land_pay')->where(['pid'=>$user['id']])->sum('pmoney');

        $money2=Db::name('user')->where(['id'=>$user['id']])->find();

        $this->success('我的金额',['all'=>$money,'now'=>round($money2['money'],2)]);

    }


    /**
     * 我的下级订单
     */
    public function getmydownorder(){



        $user=$this->auth->getUser();

        $lists=Db::name('land_pay')->where(['pid'=>$user['id']])->limit(99)->select();

        foreach ($lists as &$key){

            $key['times']=date('Y-m-d H:i',$key['times']);

            $down=Db::name('user')->where(['id'=>$key['user_id']])->find();

            $key['dname']=$down['nickname'];
        }


        $this->success('我的下级订单',$lists);

    }

    /**
     * 提交提现申请
     */
    public function tixianapply(){
        $user=$this->auth->getUserinfo();

        $userinfo=Db::name('user')->where(['id'=>$user['user_id']])->find();

        $log['name']=$this->request->param('name');
        $log['money']=$this->request->param('money');
        $log['order']=$this->request->param('order');

        if (intval($log['money'])<10){
            $this->error('提现金额错误');
        }

        if ($log['money']>$userinfo['money']){
            $this->error('提现金额错误');
        }

        if (empty($log['name'])||empty($log['order'])){
            $this->error('输入错误');
        }

        $log['times']=time();
        $log['user_id']=$user['id'];


        Db::name('tixian')->data($log)->insert();

        sendmoney($user['id'],$log['money'],'用户提现');

        $this->success('操作成功');


    }

    /**
     * 我的提现
     */

    public function getmytixian(){


        $user=$this->auth->getUserinfo();

        $list=Db::name('tixian')->where(['user_id'=>$user['id']])->select();

        foreach ($list as &$key){

            $key['times']=date('Y-m-d H:i',$key['times']);

        }

        $this->success('操作成功',$list);
    }































}